package com.situ.shoplook.common.dao;

import com.situ.shoplook.common.model.Brand;
import com.situ.shoplook.common.util.Xin;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 功能：
 *
 * @author 千堆雪
 * @version 1.0.0
 * <p>
 * created by 千堆雪 on 2024/8/26, last modified by 千堆雪 on 2024/8/26
 */
@Mapper
public interface BrandDao extends Xin.XinMapper<Brand, Integer> {
    @SelectProvider(type = Xin.class, method = "selectAll")
    List<Brand> findBy(Map<String, Object> map);

    default List<Brand> findAll(Brand brand) {
        List<String> where = new ArrayList<>();
        if (StringUtils.hasText(brand.getName())) {
            where.add("(name like #{nameLike}");
        }

        Xin.SqlSessionInfo ssi = new Xin.SqlSessionInfo(null, null, String.join(" and ", where));
        ssi.hold();
        return findBy(Map.of("nameLike", "'%" + brand.getName() + "%'"));
    }

    default Brand findById(Integer id) {
        return selectByPrimaryKey(id);
    }

    default int save(Brand brand) {
        return insert(brand);
    }

    default int update(Brand brand) {
        return updateByPrimaryKey(brand);
    }

    default int deleteByIds(Integer[] ids) {
        return deleteByPrimaryKeys(List.of(ids));
    }
}
